﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace QuickJob.forms
{
    public partial class QuickDownPictures : Form
    {

        String ShangeCheng = null;
        String HuoHao = null;
        String JiaGe = null;
        String WangZhi = null;
        String Dir = null;
        htmlOper htmlOperation = new htmlOper();

        //截图大小
        static Size sPic = new Size(400,400);


        public QuickDownPictures()
        {
            InitializeComponent();

            textBox_Dir.DoubleClick += textBox_Dir_DoubleClick;
            textBox_XiShuiMai.DoubleClick += textBox_XiShuiMai_DoubleClick;


            textBox_ShangCheng.Text = "国投 - 6F - 658";
            textBox_HuoHao.Text = "887#";
            textBox_JiaGe.Text = "￥ 33.00 ~ 42.00";
            textBox_Dir.Text = "C:\\Users\\Administrator\\Desktop\\临时文件";
            textBox_WangZhi.Text = "http://gz.17zwd.com/item.htm?GID=6216740&spm=6uqD7.42.0.2320.6216740.0";
            Dir = textBox_Dir.Text;


            htmlOperation.onDownLoadProgress += hOper_onDownLoadProgress;




        }

        void hOper_onDownLoadProgress(object sender, Ulitily.DownLoadProgressEventArg arg)
        {
            if (this.progressBar1.InvokeRequired)
            {
                Ulitily.OnDownLoadProgress odlp = new Ulitily.OnDownLoadProgress(hOper_onDownLoadProgress);
                this.progressBar1.BeginInvoke(odlp, new object[] { sender, arg });
            }
            else
            {
                this.progressBar1.Maximum = arg.Count;
                this.progressBar1.Value = arg.now;
                this.progressBar1.Update();

            }
        }

        void textBox_XiShuiMai_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.Title = "选择　洗水唛　的图片文件：xxx.jpeg  or jpg";
            if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (file.CheckFileExists == false)
                {

                    this.richTextBox1.AppendText("洗水唛文件不存在");
                }
                else
                {
                    this.textBox_XiShuiMai.Text = file.FileName;
                }
            }
        }

        void textBox_Dir_DoubleClick(object sender, EventArgs e)
        {
            FolderBrowserDialog dilog = new FolderBrowserDialog();
            dilog.Description = "请选择文件夹";
            if (dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes)
            {
                Dir = dilog.SelectedPath;
                this.textBox_Dir.Text = Dir;
            }
        }


        /// <summary>
        /// 读取信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e) {

            htmlOper.ShangPinDescription description;
            htmlOperation.HtmlLoadData(this.textBox_WangZhi.Text);
            List<string> pics = htmlOperation.HtmlGetAllPictures();
            description = htmlOperation.GetDescription();
            this.textBox_JiaGe.Text = description.jiage;
            this.textBox_ShangCheng.Text = description.shangcheng;
            this.textBox_HuoHao.Text = description.huohao;
            if (textBox_JiaGe.Text == null || this.textBox_ShangCheng.Text == null || this.textBox_HuoHao.Text == null)
            {
                this.richTextBox1.AppendText("信息出错，请检查\r\n");
            }
            if (pics != null)
                this.richTextBox1.AppendText("图片数量:" + pics.Count + " 张\r\n\r\n");

        }


        /// <summary>
        /// 下载　大图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                // EcanConvertToCh ec = new EcanConvertToCh();
                String ss = PinYinConverter.GetFirst(textBox_ShangCheng.Text); //ec.convertCh(textBox_shangCheng.Text);

                if (ss.Split('#').Length >= 2)
                {
                    ShangeCheng = ss;
                }
                else
                {

                    string[] s = ss.Split('-');

                    if (s.Length < 2)
                    {
                        throw (new Exception("商城格式错误"));
                    }

                    for (int i = 0; i < s.Length; i++)
                    {

                        if (i == 0) ShangeCheng = s[0] + "#";
                        else if (i == s.Length - 1) ShangeCheng = ShangeCheng + "#" + s[i];
                        else
                        {
                            if (i != s.Length - 2) ShangeCheng = ShangeCheng + s[i] + "-";
                            else ShangeCheng = ShangeCheng + s[i];
                        }
                    }
                }

                ShangeCheng = ShangeCheng.Replace(" ", "");
                textBox_ShangCheng.Text = ShangeCheng;

                HuoHao = textBox_HuoHao.Text.Replace(" ", "").Replace("#", "");

                JiaGe = textBox_JiaGe.Text;

                if (Dir == null) Dir = textBox_Dir.Text;


                if (Directory.Exists(Dir) == false)
                {
                    Directory.CreateDirectory(Dir);
                }



                string newPath = System.IO.Path.Combine(Dir, ShangeCheng + "#" + HuoHao + "#" + JiaGe);

                if (Directory.Exists(newPath))
                {
                    this.richTextBox1.AppendText("文件夹已经存在\r\n");
                }
                else
                {

                    Directory.CreateDirectory(newPath);
                    this.richTextBox1.AppendText("创建目录:" + newPath + "\r\n");
                }

                //添加文件 

                try
                {
                    //String enter = "\r\n";
                    //FileStream fs = File.Create(newPath + "\\" + "html.html");
                    //String html = htmlOperation.HtmlToDocumentation(this.textBox_WangZhi.Text,htmlOperation.HtmlGetTitle());
                    //byte[] data = System.Text.Encoding.Default.GetBytes(html + "\t" + "\r\n"); 
                    //fs.Write(data, 0, data.Length);
                    //fs.Close();

                    htmlOperation.HtmlSaveDocumentation(newPath);

                    ///////////////////////////////////////

                    //复制洗水唛
                    try
                    {
                        string filepath = System.IO.Path.Combine(newPath, "xishuimai.jpeg");
                        System.IO.File.Copy(this.textBox_XiShuiMai.Text, filepath);
                    }
                    catch { }

                    //加载图片
                    List<string> pics = htmlOperation.HtmlDownloadBigPictures(sPic.Width, sPic.Height, newPath,180,180);

                    foreach (string pic in pics)
                    {
                        richTextBox1.AppendText("下载：" + pic + "\r\n");
                    }

                    if (pics != null && pics.Count > 0) richTextBox1.AppendText("已下载：" + pics.Count + "张");
                    else richTextBox1.AppendText("没有大于" + sPic.Width + "* " + sPic.Height + " 的图片\r\n");


                }
                catch
                {
                    this.richTextBox1.AppendText("写入html.html 失败\r\n");
                }



            }
            catch
            {


            }
        }

        private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Clear();
        }
    }
}
